<?php

namespace app\common\model;

use think\Exception;
use think\Model;

class BusinessCoupon extends Model
{
    // 表名
    protected $name = 'business_coupon';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 追加属性
    protected $append = [
        'use_start_time_text',
        'use_end_time_text',
        'create_time_text',
        'update_time_text',
        'receive_start_time_text',
        'receive_end_time_text',
	    'status_text'
    ];
    
	const STATUS_Y = 1;
	const STATUS_N = 0;
	
	public static $statusList = [
		self::STATUS_Y => '正常',
		self::STATUS_N => '取消'
	];
    

	public function getStatusTextAttr($value, $data) {
		$value  = $value ? $value : $data['status'];
		$list = self::$statusList;
		return isset($list[$value]) ? $list[$value] : '';
	}


    public function getUseStartTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['use_start_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUseEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['use_end_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['create_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['update_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getReceiveStartTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['receive_start_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getReceiveEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['receive_end_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setUseStartTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setUseEndTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setReceiveStartTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setReceiveEndTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

	public function business(){
		return $this->belongsTo('business', 'business_id', 'id', '', 'left')->setEagerlyType(0);
	}
	
	
	public function receiveCoupon($coupon, $userId, $merId) {
		$this->startTrans();
		try {
			
			//用户领券
			$couponModel = new UserCoupon();
			$couponRes = $couponModel->isUpdate(false)
				->save([
					'user_id' => $userId,
                    'coupon_id' => $coupon['id'],
                    'name' => '店铺优惠券',
                    'amount' => $coupon['amount'],
                    'min_amount' => $coupon['min_amount'],
					'start_time' => $coupon['use_start_time'],
					'end_time' => $coupon['use_end_time'],
					'business_id' => $coupon['business_id'],
				]);
			if(!$couponRes) {
				throw new Exception('用户领券出错');
			}
			
			//更改领券数量
			$res = $this->where(['id' => $coupon['id']])
				->setInc('receive_number');
			if(!$res) {
				throw new Exception('更改领取数量出错');
			}
		
			$this->commit();
			return true;
			
		} catch (Exception $e) {
			$this->rollback();
			$this->error = $e->getMessage();
			return false;
		}
	}
}
